package cn.dg.manager.base.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.map.util.JSONPObject;

import cn.dg.manager.base.consts.Const;

/**
 * 接口参数校验
 * 
 * @author: daiwx 修改日期：2015/11/2
 */
public class AppUtil {

	protected static Logger logger = Logger.getLogger(AppUtil.class);

	/**
	 * 检查参数是否完整
	 * 
	 * @param method
	 * @param pd
	 * @return
	 */
	public static boolean checkParam(String method, PageData pd) {
		boolean result = false;

		int falseCount = 0;
		String[] paramArray = new String[20];
		String[] valueArray = new String[20];
		String[] tempArray = new String[20]; // 临时数组

		if (method == "getAppuserByUsernmae") { // 根据用户名获取会员信息
			paramArray = Const.APP_GETAPPUSER_PARAM_ARRAY;
			valueArray = Const.APP_GETAPPUSER_VALUE_ARRAY;
		} else if (method == "appUserLogin") { // 用户登录系统
			paramArray = Const.APP_USERLOGIN_PARAM_ARRAY;
			valueArray = Const.APP_USERLOGIN_VALUE_ARRAY;
		} else if (method == "changeAppUserPwd") { // 修改登录密码
			paramArray = Const.APP_CHANGEAPPUSERPWD_PARAM_ARRAY;
			valueArray = Const.APP_CHANGEAPPUSERPWD_VALUE_ARRAY;
		} else if (method == "appUserRegister") { // 用户注册
			paramArray = Const.APP_USERREGISTER_PARAM_ARRAY;
			valueArray = Const.APP_USERREGISTER_VALUE_ARRAY;
		} else if (method == "editAppUserInfo") { // 用户信息保存
			paramArray = Const.APP_EDITAPPUSERINFO_PARAM_ARRAY;
			valueArray = Const.APP_EDITAPPUSERINFO_VALUE_ARRAY;
		} else if (method == "appSendsms") { // 发送短信验证
			paramArray = Const.APP_SENDSMS_PARAM_ARRAY;
			valueArray = Const.APP_SENDSMS_VALUE_ARRAY;
		} else if (method == "appCheckVerCode") { // 检查短信验证码
			paramArray = Const.APP_CHECKVERCODE_PARAM_ARRAY;
			valueArray = Const.APP_CHECKVERCODE_VALUE_ARRAY;
		} else if (method == "editAppUserImage") { // 检查短信验证码
			paramArray = Const.APP_EDITAPPUSERIMAGE_PARAM_ARRAY;
			valueArray = Const.APP_EDITAPPUSERIMAGE_VALUE_ARRAY;
		} else if (method == "findAroundStation") {
			paramArray = Const.APP_FINDAROUNDSTATION_PARAM_ARRAY;
			valueArray = Const.APP_FINDAROUNDSTATION_VALUE_ARRAY;
		} else if (method == "getLastestStation") {
			paramArray = Const.APP_GETLASTESTSTATION_PARAM_ARRAY;
			valueArray = Const.APP_GETLASTESTSTATION_VALUE_ARRAY;
		} else if (method == "getStationDetail") {
			paramArray = Const.APP_GETSTATIONDETAIL_PARAM_ARRAY;
			valueArray = Const.APP_GETSTATIONDETAIL_VALUE_ARRAY;
		} else if (method == "searchStation") {
			paramArray = Const.APP_SEARCHSTATION_PARAM_ARRAY;
			valueArray = Const.APP_SEARCHSTATION_VALUE_ARRAY;
		} else if (method == "getorderUnpaid") {
			paramArray = Const.APP_GETORDERUNPAID_PARAM_ARRAY;
			valueArray = Const.APP_GETORDERUNPAID_VALUE_ARRAY;
		} else if (method == "getOrderByUserName") { // 查找充电账单记录
			paramArray = Const.APP_FINDORDER_PARAM_ARRAY;
			valueArray = Const.APP_FINDORDER_VALUE_ARRAY;
		} else if (method == "paySuccess") {
			paramArray = Const.APP_PAYSUCCESS_PARAM_ARRAY;
			valueArray = Const.APP_PAYSUCCESS_PARAM_ARRAY;
		} else if (method == "getPileDetail") { // 扫码获取充电桩信息
			paramArray = Const.APP_GETPILEDETAIL_PARAM_ARRAY;
			valueArray = Const.APP_GETPILEDETAIL_VALUE_ARRAY;
		} else if (method == "updOrder") {
			paramArray = Const.APP_UPDORDER_PARAM_ARRAY;
			valueArray = Const.APP_UPDORDER_VALUE_ARRAY;
		} else if (method == "chargeBegin") { // 开始充电
			paramArray = Const.APP_CDBEGIN_PARAM_ARRAY;
			valueArray = Const.APP_CDBEGIN_VALUE_ARRAY;
		} else if (method == "forgetAppUserPwd") { // 忘记密码
			paramArray = Const.APP_FORGETPWD_PARAM_ARRAY;
			valueArray = Const.APP_FORGETPWD_VALUE_ARRAY;
		} else if (method == "getUsedStation") { // 获取使用过的电站列表
			paramArray = Const.APP_GETUSEDSTATION_PARAM_ARRAY;
			valueArray = Const.APP_GETUSEDSTATION_VALUE_ARRAY;
		} else if (method == "editUserName") { // 旧手机未停用的情况下，会员修改绑定手机号
			paramArray = Const.APP_EDITUSERNAME_PARAM_ARRAY;
			valueArray = Const.APP_EDITUSERNAME_VALUE_ARRAY;
		} else if (method == "editAppName") { // 用户修改昵称
			paramArray = Const.APP_EDITNAME_PARAM_ARRAY;
			valueArray = Const.APP_EDITNAME_VALUE_ARRAY;
		} else if (method == "updatePileStatus") { // 用户修改昵称
			paramArray = Const.APP_UPDATEPILESTATUS_PARAM_ARRAY;
			valueArray = Const.APP_UPDATEPILESTATUS_VALUE_ARRAY;
		}else if (method == "checkAppuser"){//检查用户名（手机号）是否被占用
			paramArray = Const.APP_CHECKAPPUSER_PARAM_ARRAY;
			valueArray = Const.APP_CHECKAPPUSER_VALUE_ARRAY;
		} else if (method == "findVersionInfo") { //检查是否是最新版本
			paramArray = Const.APP_VERSION_PARAMETER_ARRAY;
			valueArray = Const.APP_VERSION_VALUE_ARRAY;
		} else if (method == "getChargingOrder") { //查找正在充电中的订单
			paramArray = Const.APP_CHARGING_PARAMETER_ARRAY;
			valueArray = Const.APP_CHARGING_VALUE_ARRAY;
		} else if (method == "updateOrderStatus") { //更新订单信息
			paramArray = Const.APP_UPDATEORDERSTATUS_PARAM_ARRAY;
			valueArray = Const.APP_UPDATEORDERSTATUS_VALUE_ARRAY;
		} else if (method == "findMessageList") {
			paramArray = Const.APP_MESSAGE_PARAM_ARRAY;
			valueArray = Const.APP_MESSAGE_VALUE_ARRAY;
		}

		int size = paramArray.length;
		for (int i = 0; i < size; i++) {
			String param = paramArray[i];
			if (!pd.containsKey(param)) {
				tempArray[falseCount] = valueArray[i] + "--" + param;
				falseCount += 1;
			}
		}
		if (falseCount > 0) {
			logger.error(method + "接口，请求协议中缺少 " + falseCount + "个 参数");
			for (int j = 1; j <= falseCount; j++) {
				logger.error("   第" + j + "个：" + tempArray[j - 1]);
			}
		} else {
			result = true;
		}

		return result;
	}

	/**
	 * 校验参数有效性
	 * 
	 * @param method
	 * @param pd
	 * @return
	 */
	public static String checkParamValidity(String method, PageData pd) {
		String result = Const.APP_ERROR_CODE_00;

		if (method == "appUserLogin") { // 用户登录信息校验
			String tempUserName = pd.getString("USERNAME");
			String tempPWD = pd.getString("PASSWORD");
			if (!Tools.checkMobileNumber(tempUserName)) {
				result = Const.APP_ERROR_CODE_11;
				return result;
			}
			if (Tools.isEmpty(tempPWD)) {
				result = Const.APP_ERROR_CODE_08;
				return result;
			}
		} else if (method == "changeAppUserPwd") { // 用户登录信息校验
			String tempUserName = pd.getString("USERNAME");
			String tempNewPWD = pd.getString("NEWPASSWORD");
			if (!Tools.checkMobileNumber(tempUserName)) {
				result = Const.APP_ERROR_CODE_11;
				return result;
			}
			if (Tools.isEmpty(tempNewPWD)) {
				result = Const.APP_ERROR_CODE_08;
				return result;
			}
			if (tempNewPWD.length() > 255) {
				result = Const.APP_ERROR_CODE_12;
				return result;
			}
		} else if (method == "appUserRegister") { // 用户注册校验
			String tempUserName = pd.getString("USERNAME");
			String tempPWD = pd.getString("PASSWORD");
			if (!Tools.checkMobileNumber(tempUserName)) {
				result = Const.APP_ERROR_CODE_11;
				return result;
			}
			if (Tools.isEmpty(tempPWD)) {
				result = Const.APP_ERROR_CODE_08;
				return result;
			}
			if (tempPWD.length() > 255) {
				result = Const.APP_ERROR_CODE_12;
				return result;
			}
		} else if (method == "editAppUserInfo") { // 用户保存校验
			String tempUserName = pd.getString("USERNAME");
			String tempEmail = pd.getString("EMAIL");
			if (!Tools.checkMobileNumber(tempUserName)) {
				result = Const.APP_ERROR_CODE_11;
				return result;
			}
			if (!Tools.checkEmail(tempEmail)) {
				result = Const.APP_ERROR_CODE_13;
				return result;
			}
		} else if (method == "appSendsms") { // 发送短信校验
			String temPhone = pd.getString("PHONENUMBER");
			if (!Tools.checkMobileNumber(temPhone)) {
				result = Const.APP_ERROR_CODE_11;
				return result;
			}
		} else if (method == "appCheckVerCode") { // 检查短信验证码
			String temPhone = pd.getString("PHONENUMBER");
			String temVerCode = pd.getString("MSGVERIFICATIONCODE");
			if (!Tools.checkMobileNumber(temPhone)) {
				result = Const.APP_ERROR_CODE_11;
				return result;
			}
			if (temVerCode.trim().length() != 6) {
				result = Const.APP_ERROR_CODE_14;
				return result;
			}
		} else if (method == "editAppUserImage") { // 检查路径长度
			String imgUrl = pd.getString("IMAGE");
			if (imgUrl.length() > 500) {
				result = Const.APP_ERROR_CODE_15;
				return result;
			}
		} else if (method == "getOrderByUserName") { // 检查用户名（手机号）是否合法
			String tempUserName = pd.getString("USERNAME");
			if (!Tools.checkMobileNumber(tempUserName)) {
				result = Const.APP_ERROR_CODE_11;
				return result;
			}
		} else if (method == "getApporderunpaid") { // 检查用户名是否合法

			String tempUserName = pd.getString("USERNAME");
			if (!Tools.checkMobileNumber(tempUserName)) {
				result = Const.APP_ERROR_CODE_11;
				return result;
			}
		}else if (method == "getAppuserByUm") { // 检查用户名是否合法

			String tempUserName = pd.getString("USERNAME");
			if (!Tools.checkMobileNumber(tempUserName)) {
				result = Const.APP_ERROR_CODE_11;
				return result;
			}
		}else if (method == "forgetAppUserPwd") { // 忘记密码
			String temPhone = pd.getString("USERNAME");
			String tempNewPWD = pd.getString("NEWPASSWORD");
			if (!Tools.checkMobileNumber(temPhone)) {
				result = Const.APP_ERROR_CODE_11;
				return result;
			}
			if (Tools.isEmpty(tempNewPWD)) {
				result = Const.APP_ERROR_CODE_08;
				return result;
			}
			if (tempNewPWD.length() > 255) {
				result = Const.APP_ERROR_CODE_12;
				return result;
			}
		} else if (method == "editUserName") { //修改绑定手机号
			String tempUserName = pd.getString("NEWUSERNAME");
			String temVerCode = pd.getString("MSGVERIFICATIONCODE");
			if (!Tools.checkMobileNumber(tempUserName)) {
				result = Const.APP_ERROR_CODE_11;
				return result;
			}
			if (temVerCode.trim().length() != 6) {
				result = Const.APP_ERROR_CODE_14;
				return result;
			}
		} else if (method == "checkAppuser") { // 检查用户名（手机号）是否被占用
			String tempUserName = pd.getString("USERNAME");
			if (!Tools.checkMobileNumber(tempUserName)) {
				result = Const.APP_ERROR_CODE_11;
				return result;
			}
		} else if (method == "getChargingOrder") {
			String temUserName = pd.getString("USERNAME");
			if (Tools.isEmpty(temUserName)) {
				result = Const.APP_ERROR_CODE_22;
				return result;
			} else if (!Tools.checkMobileNumber(temUserName)) {
				result = Const.APP_ERROR_CODE_11;
				return result;
			}
		}

		return result;
	}

	/**
	 * 设置分页的参数
	 * 
	 * @param pd
	 * @return
	 */
	public static PageData setPageParam(PageData pd) {
		String page_now_str = pd.get("page_now").toString();
		int pageNowInt = Integer.parseInt(page_now_str) - 1;
		String page_size_str = pd.get("page_size").toString(); // 每页显示记录数
		int pageSizeInt = Integer.parseInt(page_size_str);
		String page_now = pageNowInt + "";
		String page_start = (pageNowInt * pageSizeInt) + "";
		pd.put("page_now", page_now);
		pd.put("page_start", page_start);
		return pd;
	}

	/**
	 * 设置list中的distance
	 * 
	 * @param list
	 * @param pd
	 * @return
	 */
	public static List<PageData> setListDistance(List<PageData> list, PageData pd) {
		List<PageData> listReturn = new ArrayList<PageData>();
		String user_longitude = "";
		String user_latitude = "";
		try {
			user_longitude = pd.get("user_longitude").toString(); // "117.11811";
			user_latitude = pd.get("user_latitude").toString(); // "36.68484";
		} catch (Exception e) {
			logger.error("缺失参数--user_longitude和user_longitude");
			logger.error("lost param：user_longitude and user_longitude");
		}
		PageData pdTemp = new PageData();
		int size = list.size();
		for (int i = 0; i < size; i++) {
			pdTemp = list.get(i);
			String longitude = pdTemp.get("longitude").toString();
			String latitude = pdTemp.get("latitude").toString();
			String distance = MapDistance.getDistance(user_longitude, user_latitude, longitude, latitude);
			pdTemp.put("distance", distance);
			pdTemp.put("size", distance.length());
			listReturn.add(pdTemp);
		}
		return listReturn;
	}

	/**
	 * @param pd
	 * @param map
	 * @return
	 */
	public static Object returnObject(PageData pd, Map map) {
		if (pd.containsKey("callback")) {
			String callback = pd.get("callback").toString();
			return new JSONPObject(callback, map);
		} else {
			return map;
		}
	}
}
